package com.getpebble.android.framework.firmware;

import android.content.Context;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.core.util.ThreadUtil;
import com.getpebble.android.common.model.HardwarePlatform;
import com.getpebble.android.framework.firmware.FirmwareManifestBundle.FirmwareMetadata;
import com.getpebble.android.framework.firmware.FirmwareManifestBundle.ReleaseType;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.koushikdutta.ion.Ion;
import com.koushikdutta.ion.Response;
import com.koushikdutta.ion.builder.Builders.Any.B;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class FirmwareManifestFetcher {
    private static final String TAG = FirmwareManifestFetcher.class.getSimpleName();
    private Context mContext;
    private HardwarePlatform mHardwarePlatform;
    private ReleaseType mReleaseType;

    public FirmwareManifestFetcher(Context context, HardwarePlatform hardwarePlatform, ReleaseType releaseType) {
        this.mContext = context;
        this.mHardwarePlatform = hardwarePlatform;
        this.mReleaseType = releaseType;
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
    }

    public String getUri() {
        return String.format("https://pebblefw.s3.amazonaws.com/pebble/%s/%s/latest.json", new Object[]{this.mHardwarePlatform.getName(), this.mReleaseType.getPath()});
    }

    public FirmwareManifestBundle fetch() {
        Trace.verbose(TAG, "Getting bundle from: " + getUri());
        Response<JsonObject> jsonObjectResponse = fetchFirmwareManifest(getUri());
        if (jsonObjectResponse == null) {
            return null;
        }
        int responseCode = jsonObjectResponse.getHeaders().getResponseCode();
        if (responseCode != 200) {
            Trace.error(TAG, "Request failed: " + responseCode + " for:" + getUri());
            return null;
        }
        FirmwareManifestBundle manifest = new FirmwareManifestBundle(this.mHardwarePlatform, this.mReleaseType);
        Gson gson = new Gson();
        JsonObject jsonObject = (JsonObject) jsonObjectResponse.getResult();
        if (jsonObject == null) {
            Trace.error(TAG, "Found null jsonObject for result for " + this.mHardwarePlatform + " and " + this.mReleaseType);
            return null;
        }
        try {
            FirmwareMetadata normalMetadata = (FirmwareMetadata) gson.fromJson(jsonObject.get("normal"), FirmwareMetadata.class);
            manifest.setRecoveryMetadata((FirmwareMetadata) gson.fromJson(jsonObject.get("recovery"), FirmwareMetadata.class));
            manifest.setNormalMetadata(normalMetadata);
            return manifest;
        } catch (JsonSyntaxException jse) {
            Trace.error(TAG, "Failed to parse metadata.", jse);
            return null;
        }
    }

    public Response<JsonObject> fetchFirmwareManifest(String uri) {
        Response<JsonObject> jsonObjectResponse = null;
        if (uri == null) {
            throw new IllegalArgumentException("'uri' cannot be null!");
        }
        try {
            ThreadUtil.logAssertOnMainThread(TAG, "fetchFirmwareManifest");
            return (Response) ((B) Ion.with(this.mContext).load(uri)).asJsonObject().withResponse().get(8000, TimeUnit.MILLISECONDS);
        } catch (ExecutionException e) {
            Trace.error(TAG, "Failed to fetch firmware manifest.", e);
            return jsonObjectResponse;
        } catch (InterruptedException e2) {
            Trace.error(TAG, "Failed to fetch firmware manifest.", e2);
            return jsonObjectResponse;
        } catch (TimeoutException e3) {
            Trace.error(TAG, "Failed to fetch firmware manifest.", e3);
            return jsonObjectResponse;
        }
    }
}
